package icu.zhhll.util.math;

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * 计算比率
 *
 * @author zh
 * @date 2023/8/11 10:16
 */
public class CalculationRatio {
    public static final DecimalFormat df = new DecimalFormat("0.00%");
    public static final DecimalFormat sdf = new DecimalFormat("+0.00%");

    // 默认精度
    private static final int DEFAULT_SCALE = 4;

    /**
     * 日环比 返回百分比格式的字符串
     *
     * @param yesterdayCount       昨天的数量
     * @param beforeYesterdayCount 前天的数量
     * @param retainPercent        是否保留百分号
     * @return
     */
    public static String dayToDayRatioForPercentage(BigDecimal yesterdayCount, BigDecimal beforeYesterdayCount,
                                                    boolean retainPercent) {
        return dayToDayRatioForPercentage(yesterdayCount, beforeYesterdayCount, retainPercent, DEFAULT_SCALE);
    }

    public static String dayToDayRatioForPercentage(BigDecimal yesterdayCount, BigDecimal beforeYesterdayCount,
                                                    boolean retainPercent, int scale) {
        String ratio = dayToDayRatioForPercentage(yesterdayCount, beforeYesterdayCount, scale);
        // 不保留百分号并且返回的比率中存在百分号
        if (!retainPercent && ratio.contains("%")) {
            return StringUtils.remove(ratio, "%");
        } else {
            return ratio;
        }
    }


    /**
     * 日环比 返回百分比格式的字符串
     *
     * @param yesterdayCount       昨天的数量
     * @param beforeYesterdayCount 前天的数量
     * @return
     */
    public static String dayToDayRatioForPercentage(BigDecimal yesterdayCount, BigDecimal beforeYesterdayCount) {
        return dayToDayRatioForPercentage(yesterdayCount, beforeYesterdayCount, DEFAULT_SCALE);
    }

    public static String dayToDayRatioForPercentage(BigDecimal yesterdayCount, BigDecimal beforeYesterdayCount, int scale) {
        if (yesterdayCount.compareTo(BigDecimal.ZERO) == 0 || beforeYesterdayCount.compareTo(BigDecimal.ZERO) == 0) {
            return "--";
        } else {
            BigDecimal ratio = dayToDayRatio(yesterdayCount, beforeYesterdayCount, scale);
            return df.format(ratio);
        }
    }

    /**
     * 日环比(有符号位) 返回百分比格式的字符串
     *
     * @param yesterdayCount       昨天的数量
     * @param beforeYesterdayCount 前天的数量
     * @param retainPercent        是否保留百分号
     * @return
     */
    public static String dayToDaySymbolRatioForPercentage(BigDecimal yesterdayCount, BigDecimal beforeYesterdayCount,
                                                          boolean retainPercent) {
        return dayToDaySymbolRatioForPercentage(yesterdayCount, beforeYesterdayCount, retainPercent, DEFAULT_SCALE);
    }

    public static String dayToDaySymbolRatioForPercentage(BigDecimal yesterdayCount, BigDecimal beforeYesterdayCount,
                                                          boolean retainPercent, int scale) {
        String ratio = dayToDaySymbolRatioForPercentage(yesterdayCount, beforeYesterdayCount, scale);
        // 不保留百分号并且返回的比率中存在百分号
        if (!retainPercent && ratio.contains("%")) {
            return StringUtils.remove(ratio, "%");
        } else {
            return ratio;
        }
    }


    /**
     * 日环比(有符号位) 返回百分比格式的字符串
     *
     * @param yesterdayCount       昨天的数量
     * @param beforeYesterdayCount 前天的数量
     * @return
     */
    public static String dayToDaySymbolRatioForPercentage(BigDecimal yesterdayCount, BigDecimal beforeYesterdayCount) {
        return dayToDaySymbolRatioForPercentage(yesterdayCount, beforeYesterdayCount, DEFAULT_SCALE);
    }

    public static String dayToDaySymbolRatioForPercentage(BigDecimal yesterdayCount, BigDecimal beforeYesterdayCount, int scale) {
        if (yesterdayCount.compareTo(BigDecimal.ZERO) == 0 || beforeYesterdayCount.compareTo(BigDecimal.ZERO) == 0) {
            return "--";
        } else {
            BigDecimal ratio = dayToDayRatio(yesterdayCount, beforeYesterdayCount, scale);
            if (ratio.compareTo(BigDecimal.ZERO) > 0) {
                return sdf.format(ratio);
            }
            return df.format(ratio);
        }
    }

    public static String dayToDaySymbolRatioForPercentage(int yesterdayCount, int beforeYesterdayCount) {
        return dayToDaySymbolRatioForPercentage(BigDecimal.valueOf(yesterdayCount), BigDecimal.valueOf(beforeYesterdayCount), DEFAULT_SCALE);
    }

    public static String dayToDaySymbolRatioForPercentage(int yesterdayCount, int beforeYesterdayCount, int scale) {
        return dayToDaySymbolRatioForPercentage(BigDecimal.valueOf(yesterdayCount), BigDecimal.valueOf(beforeYesterdayCount), scale);
    }

    public static String dayToDaySymbolRatioForPercentage(long yesterdayCount, long beforeYesterdayCount) {
        return dayToDaySymbolRatioForPercentage(BigDecimal.valueOf(yesterdayCount), BigDecimal.valueOf(beforeYesterdayCount), DEFAULT_SCALE);
    }

    public static String dayToDaySymbolRatioForPercentage(long yesterdayCount, long beforeYesterdayCount, int scale) {
        return dayToDaySymbolRatioForPercentage(BigDecimal.valueOf(yesterdayCount), BigDecimal.valueOf(beforeYesterdayCount), scale);
    }

    /**
     * 日环比
     *
     * @param yesterdayCount       昨天的数量
     * @param beforeYesterdayCount 前天的数量
     * @return
     */
    public static BigDecimal dayToDayRatio(BigDecimal yesterdayCount, BigDecimal beforeYesterdayCount) {
        return dayToDayRatio(yesterdayCount, beforeYesterdayCount, DEFAULT_SCALE);

    }

    public static BigDecimal dayToDayRatio(BigDecimal yesterdayCount, BigDecimal beforeYesterdayCount, int scale) {
        if (yesterdayCount.compareTo(BigDecimal.ZERO) == 0 || beforeYesterdayCount.compareTo(BigDecimal.ZERO) == 0) {
            return BigDecimal.ZERO;
        } else {
            return yesterdayCount
                    .divide(beforeYesterdayCount, scale, BigDecimal.ROUND_HALF_UP)
                    .subtract(BigDecimal.valueOf(1));
        }

    }

    /**
     * 获取比率 返回百分比格式的字符串
     *
     * @param dividend      被除数
     * @param divisor       除数
     * @param retainPercent 是否保留百分号
     * @return
     */
    public static String getRatioForPercentage(BigDecimal dividend, BigDecimal divisor, boolean retainPercent) {
        String ratio = getRatioForPercentage(dividend, divisor);
        // 不保留百分号并且返回的比率中存在百分号
        if (!retainPercent && ratio.contains("%")) {
            return StringUtils.remove(ratio, "%");
        } else {
            return ratio;
        }
    }

    /**
     * 获取比率 返回百分比格式的字符串
     *
     * @param dividend      被除数
     * @param divisor       除数
     * @param retainPercent 是否保留百分号
     * @return
     */
    public static String getRatioForPercentage(int dividend, int divisor, boolean retainPercent) {
        String ratio = getRatioForPercentage(dividend, divisor);
        // 不保留百分号并且返回的比率中存在百分号
        if (!retainPercent && ratio.contains("%")) {
            return StringUtils.remove(ratio, "%");
        } else {
            return ratio;
        }
    }

    /**
     * 获取比率 返回百分比格式的字符串
     *
     * @param dividend 被除数
     * @param divisor  除数
     * @return 返回百分比
     */
    public static String getRatioForPercentage(BigDecimal dividend, BigDecimal divisor) {
        return getRatioForPercentage(dividend, divisor, DEFAULT_SCALE);
    }

    public static String getRatioForPercentage(BigDecimal dividend, BigDecimal divisor, int scale) {
        if (divisor.compareTo(BigDecimal.ZERO) == 0) {
            return "--";
        } else {
            BigDecimal ratio = getRatio(dividend, divisor, scale);
            return df.format(ratio);
        }
    }

    /**
     * 获取比率 返回百分比格式的字符串
     *
     * @param dividend 被除数
     * @param divisor  除数
     * @return 返回百分比
     */
    public static String getRatioForPercentage(long dividend, long divisor) {
        return getRatioForPercentage(BigDecimal.valueOf(dividend), BigDecimal.valueOf(divisor));
    }


    /**
     * 获取比率 返回百分比格式的字符串
     *
     * @param dividend 被除数
     * @param divisor  除数
     * @return 返回百分比
     */
    public static String getRatioForPercentage(int dividend, int divisor) {
        return getRatioForPercentage(BigDecimal.valueOf(dividend), BigDecimal.valueOf(divisor));
    }


    /**
     * 获取比率(有符号位) 返回百分比格式的字符串
     *
     * @param dividend 被除数
     * @param divisor  除数
     * @return 返回百分比
     */
    public static String getSymbolRatioForPercentage(BigDecimal dividend, BigDecimal divisor) {
        return getSymbolRatioForPercentage(dividend, divisor, DEFAULT_SCALE);
    }

    public static String getSymbolRatioForPercentage(BigDecimal dividend, BigDecimal divisor, int scale) {
        if (divisor.compareTo(BigDecimal.ZERO) == 0) {
            return "--";
        } else {
            BigDecimal ratio = getRatio(dividend, divisor, scale);
            if (ratio.compareTo(BigDecimal.ZERO) > 0) {
                return sdf.format(ratio);
            }
            return df.format(ratio);
        }
    }

    public static String getSymbolRatioForPercentage(int dividend, int divisor) {
        return getSymbolRatioForPercentage(BigDecimal.valueOf(dividend), BigDecimal.valueOf(divisor));
    }

    /**
     * 获取比率
     *
     * @param dividend 被除数
     * @param divisor  除数
     * @return
     */
    public static BigDecimal getRatio(BigDecimal dividend, BigDecimal divisor) {
        return getRatio(dividend, divisor, DEFAULT_SCALE);
    }

    /**
     * 获取比率
     *
     * @param dividend 被除数
     * @param divisor  除数
     * @return
     */
    public static BigDecimal getRatio(BigDecimal dividend, BigDecimal divisor, int scale) {
        if (divisor.compareTo(BigDecimal.ZERO) == 0) {
            return BigDecimal.ZERO;
        } else {
            return dividend.divide(divisor, scale, BigDecimal.ROUND_HALF_UP);
        }
    }

    /**
     * 获取比率
     *
     * @param dividend 被除数
     * @param divisor  除数
     * @return
     */
    public static BigDecimal getRatio(long dividend, long divisor) {
        return getRatio(BigDecimal.valueOf(dividend), BigDecimal.valueOf(divisor), DEFAULT_SCALE);
    }

    public static BigDecimal getRatio(long dividend, long divisor, int scale) {
        return getRatio(BigDecimal.valueOf(dividend), BigDecimal.valueOf(divisor), scale);
    }

    /**
     * 获取比率(有符号位) 返回百分比格式的字符串
     *
     * @param dividend      被除数
     * @param divisor       除数
     * @param retainPercent 是否保留百分号
     * @return
     */
    public static String getSymbolRatioForPercentage(BigDecimal dividend, BigDecimal divisor, boolean retainPercent) {
        String ratio = getSymbolRatioForPercentage(dividend, divisor);
        // 不保留百分号并且返回的比率中存在百分号
        if (!retainPercent && ratio.contains("%")) {
            return StringUtils.remove(ratio, "%");
        } else {
            return ratio;
        }
    }
}
